package cn.lirenyi.org.service.impl;

import cn.lirenyi.basic.exception.BusinessException;
import cn.lirenyi.basic.service.impl.BaseServiceImpl;
import cn.lirenyi.org.domain.Employee;
import cn.lirenyi.org.domain.Shop;
import cn.lirenyi.org.mapper.EmployeeMapper;
import cn.lirenyi.org.mapper.ShopMapper;
import cn.lirenyi.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void settlement(Shop shop) {
        // 获取admin
        Employee admin = shop.getAdmin();
        // 判断admin里面的密码和确认密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            // 如果不相等就返回一个自定义异常类
            throw new BusinessException("两次密码不一致，吃屎把你！");
        }
        // 校验账号是否存在,需要校验三个字段，username，phone，email
        Employee empusername = employeeMapper.queryUser(admin.getUsername());
            if (empusername != null){
                throw new BusinessException("该账号已存在，请重试");
            }
        // 保存员工信息，由于店铺admin_id不能为null，所以应该先保存员工返回自增id
        employeeMapper.add(admin);
        // 把admin设置成管理员
        shop.setAdmin(admin);
        // 返回自增id
        shopMapper.add(shop);
        // 修改店长所属的店铺的id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }































}

